package com.pcxm.cm.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.pcxm.cm.pojo.SysMenu;
import com.pcxm.cm.service.SysMenuService;
import com.pcxm.cm.util.SfJson;

/**
 * @author Administrator
 * 
 */

public class SysMenuAction extends ActionSupport {

	private SysMenu SysMenu;
	private static final Log log = LogFactory.getLog(SysMenuAction.class);
	private String roleId;
	private String userId;
	private String jsonStr;
	private SysMenuService sysMenuService;
	
	/**
	 * 获取导航树
	 * 
	 * @return
	 */
	public String loadSysMenuTrees() {
		// List<SysMenu> list = service.findAllSysMenu(roleId);
		List<SysMenu> list = sysMenuService.getSysMenuTree(); // 获取sys_menu中数据
		List topList = findTopMenus(list);
		for (int i = 0, l = topList.size(); i < l; i++) {
			SysMenu topInfo = (SysMenu) topList.get(i);
			findSubMenus(topInfo, list);// 添加子节点
		}
		StringBuilder json = new StringBuilder(
				"{\"success\":true,\"children\":[");
		for (int l = topList.size() - 1; l >= 0; l--) {
			SysMenu topInfo = (SysMenu) topList.get(l);
			String jsonStr = buildSysMenuTreeJson(topInfo);
			json.append(jsonStr);
			if (l > 0) {
				json.append(",");
			}
		}
		json.append("]}");
		this.setJsonStr(json.toString());
		System.out.println(json.toString());
		return SUCCESS;
	}

	/**
	 * 获取菜单表格
	 * 
	 * @return
	 */
	public String loadSysMenuGrid() {
		// List<SysMenu> list = service.findAllSysMenu(roleId);
		List<SysMenu> list = sysMenuService.getSysMenuList(); // 获取sys_menu中数据
		String jsonlist = SfJson.ListToJson(list);
		String json = "{\"rel\": true, \"msg\": \"获取成功\", \"list\": ";
		json += jsonlist;
		json += ", \"count\": " + list.size() + " }";

		this.setJsonStr(json);
		System.out.println(json);
		return SUCCESS;
	}

	private List<SysMenu> findTopMenus(List<SysMenu> list) {
		List<SysMenu> topList = new ArrayList<SysMenu>();
		for (int i = list.size() - 1; i >= 0; i--) {
			SysMenu info = (SysMenu) list.get(i);
			String preId = info.getPreId();
			if (preId == null || preId.isEmpty()) {
				topList.add(info);
				list.remove(i);
			}
		}
		return topList;
	}

	private void findSubMenus(SysMenu info, List<SysMenu> list) {
		for (int i = list.size() - 1; i >= 0; i--) {
			SysMenu subInfo = (SysMenu) list.get(i);
			if (subInfo.getPreId().equals(info.getId())) {
				info.addChildren(subInfo);
				// list.remove(i);
				findSubMenus(subInfo, list);
			}
		}
	}

	private String buildSysMenuTreeJson(SysMenu info) {
		List childList = info.getChildren();

		StringBuilder jsonStr = new StringBuilder("{");

		jsonStr.append("\"title\":").append("\"").append(info.getName())
				.append("\","); // title
		jsonStr.append("\"icon\":").append("\"fa-").append(info.getIconPath())
				.append("\","); // icon
		jsonStr.append("\"href\":").append("\"").append(info.getPath())
				.append("\""); // href

		if (childList != null && !childList.isEmpty() && childList.size() != 0) {
			jsonStr.append(",\"spread\": false,\"children\":[");
			for (int i = childList.size() - 1; i >= 0; i--) {
				SysMenu child = (SysMenu) childList.get(i);
				String childJson = buildSysMenuTreeJson(child);
				jsonStr.append(childJson);
				if (i > 0) {
					jsonStr.append(",");
				}
			}
			jsonStr.append("]}");
		} else {
			jsonStr.append("}");
		}

		return jsonStr.toString();
	}

	
	public SysMenu getSysMenu() {
		return SysMenu;
	}

	public void setSysMenu(SysMenu sysMenu) {
		SysMenu = sysMenu;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public SysMenuService getSysMenuService() {
		return sysMenuService;
	}

	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}


}
